package org.peoples.ui.containers {
	
	import flash.display.GradientType;
	import flash.geom.Matrix;
	
	import mx.containers.Canvas;
	import flash.display.Sprite;
	
	[Style(name="gradientAlphas", type="Array", arrayType="Number", inherit="yes")]
	[Style(name="gradientColors", type="Array", arrayType="uint", format="Color", inherit="yes")]
	public class GradientBar extends Canvas {
		
		private var gradientChanged:Boolean = true;
		private var mc_gradient:Sprite;
		
		public function GradientBar() {
			
			super();
			
		}
		
		override protected function createChildren():void {
			super.createChildren();
			
			if (!mc_gradient) {
				mc_gradient = new Sprite();
				mc_gradient.name = "mc_gradient";
				rawChildren.addChildAt(mc_gradient, 0);
			}
		}
		
		private function drawGradient():void {
			var alphas:Array = getStyle("gradientAlphas");
			var colors:Array = getStyle("gradientColors");
			
			if (alphas != null && colors != null) {
				var matr:Matrix = new Matrix();
				matr.createGradientBox(unscaledWidth, unscaledHeight, Math.PI / 2);
				
				mc_gradient.graphics.clear();
				mc_gradient.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, [0, 255], matr);
				mc_gradient.graphics.drawRect(0, 0, unscaledWidth, unscaledHeight);
			}
		}
		
		override public function setActualSize(w:Number, h:Number):void {
			gradientChanged = true;
			super.setActualSize(w, h);
		}
		
		override public function styleChanged(styleProp:String):void {
			super.styleChanged(styleProp);
			
			if (styleProp == "gradientAlphas" || styleProp == "gradientColors") {
				gradientChanged = true;
				return;
			}
		}
		
		override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
			super.updateDisplayList(unscaledWidth, unscaledHeight);
			
			if (gradientChanged) {
				gradientChanged = false;
				drawGradient();
			}
		}
		
		override public function set height(value:Number):void {
			gradientChanged = true;
			super.height = value;
		}
		
		override public function set width(value:Number):void {
			gradientChanged = true;
			super.width = value;
		}
		
	}
	
}